# -*- coding: utf-8 -*-
"""
@Time : 2023/6/10 23:24
@Author : Gremore
@File : test
@Des: 
"""


from fastapi import Depends, HTTPException, Request

from fastapp.database.redis import sys_cache
from aioredis import Redis

async def say_hello():
    return {"message": "Hello World"}


# 方式二：连接池放在依赖注入
async def test_redis_depends(today:int, cache: Redis = Depends(sys_cache)):
    await cache.set(name="today", value=today)
    value = await cache.get("today")
    return {"message": f"今天是{today}号, data={value}"}

# 方式一：连接池放在request
async def test_redis(req: Request, today:int):
    await req.app.state.cache.set(name="today", value=today)
    value = await req.app.state.cache.get("today")
    return {"message": f"今天是{today}号, data={value}"}


from tsim.models.user import UserTab
from tsim.models import Session
async def test_readdb(req: Request):
    session = Session()
    # 查询数据
    query = session.query(UserTab).all()  # 返回全部
    print(query)
    result = session.query(UserTab).filter(UserTab.username == 'admin').first()  # 返回首个
    print(result)
    return {
        "code": 200,
        "message": query}